package com.iris.live.services.report.sources;

import com.google.common.collect.Maps;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.models.ReportConfig;
import com.iris.live.services.report.data.BaseReportDataSource;
import com.iris.live.services.report.data.ReportContext;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by HangLiu on 2017/8/12.
 */
@Component("FinanceManagementDataSource")
public class FinanceManagementDataSource extends BaseReportDataSource {

    @Override
    public List getData(ReportContext context, List<ReportConfig> config) throws Exception {
        Map info = context.getParams();
        String lineType = info.get("displayType").toString();
        List<Map> datas = new ArrayList<>();
        if ("1".equals(lineType)) {
            datas = getTimeLineData(info);
        } else {
            datas = getOrgLineData(info);
        }

        super.brandGroup(context, datas);
        return datas;

    }

    private List getTimeLineData(Map info) {
        String sql = " SELECT vehicle_brand 品牌, substring(stat_date, 5,2) date, " + this.getIndicatorColumns() +
                "    from LSH经销商表 a, t_bi_monthly_stat_data b   " +
                "    where  a.许可_BMBS = b.permit and 1=1 and b.stat_date >= date_format(:startDay, '%Y%m') and b.stat_date <= date_format(:endDay, '%Y%m') " +
                "    group by 品牌, date ";

        Map map = Maps.newHashMap();

        String permit = info.get("permit") == null ? "" : info.get("permit").toString();
        if(! StringUtils.isNullOrBlank(permit) && !"LSH".equals(permit)){
            sql = sql.replace("1=1", "  (a.大区= :permit  or  a.大区_EN=:permit or  a.CTA=:permit or  a.CTA_EN=:permit  or b.permit=:permit) ");
            map.put("permit", info.get("permit"));
        }

        map.put("startDay", info.get("startDay"));
        map.put("endDay", info.get("endDay"));
        List resultSet = this.getList(sql, map);
        return resultSet;
    }

    private List getOrgLineData(Map info) {
        String sql = "select  'DCH' 集团,a.许可_LSH,a.大区 AS 大区, a.大区_EN AS 大区_EN, a.小区 AS 小区, a.小区_EN AS 小区_EN, " +
                "    IF ( isnull(a.CTA) OR a.CTA = '', '其他cta', a.CTA ) AS CTA,  " +
                "    IF (isnull(a.CTA_EN) OR a.CTA_EN = '', 'Other CTA', a.CTA_EN ) AS CTA_EN, " +
                "    a.许可_BMBS AS 许可_BMBS, " +
                "    a.经销商名 AS 经销商名, " +
                "    a.经销商名_EN AS 经销商名_EN, " +
                "    a.SalesCode AS salesCode, b.vehicle_brand 品牌, " +
                this.getIndicatorColumns() +
                "    from LSH经销商表 a, t_bi_monthly_stat_data b   " +
                "    where  a.许可_BMBS = b.permit and 1=1 and b.stat_date >= date_format(:startDay, '%Y%m') and b.stat_date <= date_format(:endDay, '%Y%m') " +
                "    group by 品牌, permit ";
        Map map = Maps.newHashMap();
        map.put("startDay", info.get("startDay"));
        map.put("endDay", info.get("endDay"));
        return this.getList(sql, map);
    }

    private String getIndicatorColumns() {
        return  " SUM(IFNULL(financial_service_fee_amount,0)) * 1.17 金融服务费, " +
                "  SUM(IFNULL(financial_commission_amount,0)) * 1.06 金融手续费, " +
                "  SUM(IFNULL(financial_vehicle_num,0)) 金融台数, " +
                "  SUM(IFNULL(financial_vehicle_num,0)) 金融总台数, " +
                "  SUM(IFNULL(vehicle_sale_volumn,0)) 新车销量, " +
                "  SUM(IFNULL(financial_load_amount,0)) 贷款总额 ";
    }
}
